/*
 * Copyright (c) 2013, FPX and/or its affiliates. All rights reserved.
 * Use, Copy is subject to authorized license.
 */
package com.open.tools;

import java.util.HashMap;
import java.util.Map;

/**
 * 
 * 应用上下文
 *
 */
public class AppContext {

    /**
     * threadLocal对象
     */
    private static final ThreadLocal<Map<String, Object>> THREAD_LOCAL = new ThreadLocal<Map<String, Object>>();

    /**
     * 设置对象
     * 
     * @param name
     * @param value
     */
    public static void set(String name, Object value) {
        if (name != null) {
            Map<String, Object> map = THREAD_LOCAL.get();
            if (map == null) {
                map = new HashMap<String, Object>();
                THREAD_LOCAL.set(map);
            }
            THREAD_LOCAL.get().put(name, value);
        }
    }

    /**
     * 获取对象
     * 
     * @return 对象
     */
    public static Object get(String name) {
        Object r = null;
        Map<String, Object> map = THREAD_LOCAL.get();
        if (map != null) {
            r = map.get(name);
        }
        return r;
    }

    /**
     * 获取对象
     * 
     * @param name
     * @return
     */
    public static String getStr(String name) {
        String r = null;

        Object object = get(name);
        if (object != null) {
            r = String.valueOf(object);
        }

        return r;
    }

    /**
     * 清空对象
     * 
     * @param
     * @return
     */
    public static void clear() {
        Map<String, Object> map = THREAD_LOCAL.get();
        if (map != null) {
            map.clear();
        }
    }
}
